package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.common.excpetion.CustException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 点赞业务层实现
 */
@Service("ApLikesBehaviorService")
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //1. 校验参数
        ApUser user = AppThreadLocalUtils.getUser();

        //如果都为null
        if (user == null && dto.getEquipmentId() == null)
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);

        // 用户使用的账号登录
        if (user != null) {
            // TODO 增加对登录的校验
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            //用户执行的点赞操作
            if (dto.getOperation() == 0) {
                // 根据用户Id和文章Id查询点赞实体类
                ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(Query.query(
                                Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId())),
                        ApLikesBehavior.class);

                // 已经点过赞
                if (apLikesBehavior != null)
                    CustException.cust(AppHttpCodeEnum.PARAM_INVALID);

                // 向MongoDB插入点赞数据
                {
                    ApLikesBehavior behavior = new ApLikesBehavior();
                    behavior.setEntryId(String.valueOf(apBehaviorEntry.getId()));
                    behavior.setCreatedTime(new Date());
                    behavior.setOperation((short) 0);
                    behavior.setArticleId(dto.getArticleId());
                    behavior.setType(dto.getType());
                    mongoTemplate.save(behavior);
                }


            } else {
                // 根据用户ID删除该条数据
                mongoTemplate.remove(Query.query(
                                Criteria.where("entryId").is(apBehaviorEntry.getId())),
                        ApLikesBehavior.class);
            }
            // 根据用户执行的操作向MQ发送点赞情况消息
            {
                UpdateArticleMess articleMess = new UpdateArticleMess();
                articleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
                //如果是0 ->点赞操作 ->设置增加属性为1
                articleMess.setAdd(dto.getOperation() == 0 ? 1 : -1);
                articleMess.setArticleId(dto.getArticleId());
                //发送消息
                rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(articleMess));
            }
        } else {
            // 用户使用的设备Id登录 拒绝操作
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请登录后再操作");
        }

        return ResponseResult.okResult();
    }
}
